package com.bwc.aidemo.amap

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bwc.aidemo.R
import com.mojawa.mj_amap.item.SubsectionDistionItem

class AmapAdapter: RecyclerView.Adapter<AmapAdapter.SubsectionViewHolder>() {
    private val kilometerDataList = mutableListOf<SubsectionDistionItem>()
    // ViewHolder 类，用来持有 View 引用
    class SubsectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val kilometre: TextView = itemView.findViewById(R.id.kilometre)
        val speedAllocation: TextView = itemView.findViewById(R.id.speedAllocation)
        val cumulativeDuration: TextView = itemView.findViewById(R.id.cumulativeDuration)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubsectionViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_run_subsection, parent, false)
        return SubsectionViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return kilometerDataList.size
    }

    override fun onBindViewHolder(holder: SubsectionViewHolder, position: Int) {
        val currentItem = kilometerDataList[position]
        holder.kilometre.text = currentItem.kilometre.toString()
        holder.speedAllocation.text = currentItem.speedAllocation
        holder.cumulativeDuration.text = currentItem.cumulativeDuration
    }

    // 添加新公里数据
    fun addKilometerData(kilometerData: SubsectionDistionItem) {
        kilometerDataList.add(kilometerData)
        notifyDataSetChanged()
    }
}